<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use app\index\model\ActivityModel;
use think\Request;
use think\Session;

class Activity extends Controller
{
    //添加活动页面
    public function activityAdd()
    {
        $id = Session::get('id');
        $username = Session::get('username');
//        halt($id);
        if(request()->isPost()){
            $param = input('post.');
//            halt($param);

            unset($param['file']);
            $param['add_time'] = date('Y-m-d H:i:s');
            //判断活动类型 如果是转发再判断是否发布微博
            if ($param['type']==1){
                $data = Db::table('wb_message')->field('content')->where('uid',$id)->select();
                if (!empty($data)){
                    $article = new ActivityModel();
                    $flag = $article->addActivity($param);
                    return json(msg($flag['code'], $flag['data'], $flag['msg']));
                }else{
                    return msg(1, url('/server'), '请先发布微博');
                }
            }else{
                $article = new ActivityModel();
                $flag = $article->addActivity($param);
                return json(msg($flag['code'], $flag['data'], $flag['msg']));
            }
        }
        $p = Db::field('photo')
            ->table(['wb_buserinfo'])
            ->where('uid = '.$id)
            ->find();
        $photo = implode("",$p);
        return $this->fetch('index@activity/activity',[
            'uid'=>$id,
            'username'=>$username,
            'photo'=>$photo
        ]);
    }

    // 上传缩略图
    public function uploadImg()
    {
//        halt(request()->isAjax());
        if(request()->isAjax()){

            $file = request()->file('file');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
            if($info){
                $src =  '/upload' . '/' . date('Ymd') . '/' . $info->getFilename();
                return json(msg(0, ['src' => $src], ''));
            }else{
                // 上传失败获取错误信息
                return json(msg(-1, '', $file->getError()));
            }
        }
    }
    // 服务活动列表
    public function server($uid)
    {
//        halt(input());
        if (!empty($uid)){
            $id =$uid;
            $res = Db::table(['wb_buser'=>'b','wb_buserinfo'=>'f'])->field(['b.username,f.photo'])->where('b.id=f.uid and b.id='.$id)->find();
//            halt($res);
            $pto = $res['photo'];
            $sname = $res['username'];
            $username = Session::get('username');
//            halt($sname);
            $list = Db::field(['b.username', 'b.id','a.title','a.description','a.keywords','a.thumbnail','a.content','a.add_time','a.uid','a.status','a.type'])
                ->table(['wb_buser'=>'b','wb_articles'=>'a'])
                ->where(' b.id = a.uid and b.id= '.$id)->select();
//        halt($list);
            $uid = Session::get('id');
            $p = Db::field('photo')
                ->table(['wb_buserinfo'])
                ->where('uid = ' . $uid)
                ->find();
            $photo = implode("", $p);

            return view('index@activity/server', [
                'list' => $list,
                'username'=>$username,
                'id' => $uid,
                'photo'=>$photo,
                'sname'=>$sname,
                'pto'=>$pto
            ]);
        }else{
            $id = Session::get('id');
            $username = Session::get('username');
            $sname = Session::get('username');
            $list = Db::field(['b.username', 'b.id','a.title','a.description','a.keywords','a.thumbnail','a.content','a.add_time','a.uid','a.status','a.type'])
                ->table(['wb_buser'=>'b','wb_articles'=>'a'])
                ->where(' b.id = a.uid and b.id= '.$id)->select();
    //        halt($list);
            $uid = Session::get('id');
            $p = Db::field('photo')
                ->table(['wb_buserinfo'])
                ->where('uid = ' . $uid)
                ->find();
    //        halt($p);
            $pto = $p['photo'];
    //        halt($pto);
            $photo = implode("", $p);

            return view('index@activity/server', [
                'list' => $list,
                'username'=>$username,
                'id' => $uid,
                'photo'=>$photo,
                'sname'=>$sname,
                'pto'=>$pto
            ]);
        }


    }
}